import { PetNewBaseView } from "./PetNewBaseView";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { SPetInfo } from "../../../../Message/Game/SPetInfo";
import { PetUtil } from "./info/PetUtil";
import { PetColor } from "./info/PetColor";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { DataEvent } from "../../events/DataEvent";
import { DragEvent } from "../../../../com/mui/events/DragEvent";
import { PetItem } from "./PetItem";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { SPetUpdate } from "../../../../Message/Game/SPetUpdate";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { Cache } from "../../cache/Cache";
import { AIManager } from "../../scene/ai/AIManager";
import { ENpcEffect } from "../../../../Message/Public/ENpcEffect";
import { ShopConfig } from "../../resource/ShopConfig";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ObjCreate } from "../common/ObjCreate";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { ImagesConst } from "../../resource/ImagesConst";
import { PetComboBoxItemCellRender } from "./changeModel/PetComboBoxItemCellRender";
import { BuyCommonPanel } from "./skill/BuyCommonPanel";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { PetModelPanel } from "./PetModelPanel";
import { RadioButtonGroup } from "../../../../fl/controls/RadioButtonGroup";
import { PublicMessageConfig } from "../../resource/PublicMessageConfig";
import { FlyFootBmp } from "../mapUI/flyFoot/FlyFootBmp";
import { AIType } from "../../scene/ai/AIType";
import { GComboBox } from "../../../../com/mui/controls/GComboBox";
import { GRadioButton } from "../../../../com/mui/controls/GRadioButton";
import { GButton } from "../../../../com/mui/controls/GButton";
import { ShopItemInfo } from "../../resource/info/ShopItemInfo";
import { TaskTargetInfo } from "../../model/TaskTargetInfo";
import { ItemData } from "../../resource/info/ItemData";
type int = number;
//class PetChangeModelPanel
    
    export  class PetChangeModelPanel extends PetNewBaseView
    {
        constructor()
        {
            super();
            Dispatcher.addEventListener(EventName.PetChangePetModelSuccess, this.onChangeModelSuccessHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            return;
        }

        public /* override */ update(arg1: Object, ... rest): void
        {
            super.update(arg1, rest);
            this.clear();
            this.resetComboBoxData();
            return;
        }

        /* internal  */updateMainByPetInfo(arg1: SPetInfo): void
        {
            this._mainPetInfo = arg1;
            if (arg1) 
            {
                this._mainPetItem.dragSource = arg1;
                this._mainPetModel.updateModelByModelId(PetUtil.getShowModelId(arg1));
                this._mainPetModel.visible = true;
                this._mainPetComboBox.selectedIndex = this.getPetInComboIndex(arg1);
                this._mainPetComboBox.textColor = PetColor.getTalentColor(arg1).intColor;
            }
            else 
            {
                this._mainPetModel.visible = false;
            }
            this.onRadioChangeHandler(null);
            return;
        }

        /* internal  */onRadioChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=null;
            var loc2=0;
            if (this._mainPetInfo && this._deputyPetInfo) 
            {
                loc2 = this._radioGroup.selectedData as int;
                if (loc2 != 0) 
                {
                    loc1 = this._deputyPetInfo;
                }
                else 
                {
                    loc1 = this._mainPetInfo;
                }
                this._deputyPetModel.updateModelByModelId(PetUtil.getShowModelId(loc1));
            }
            return;
        }

        /* internal  */onChangeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=0;
            var loc2=null;
            if (this._mainPetInfo && this._deputyPetInfo) 
            {
                loc1 = this._buyPanel.packItemAmount;
                loc2 = this._buyPanel.itemDataArray;
                if (loc1 == 0) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54199) + ItemsUtil.getItemName(this._buyPanel.itemData));
                    return;
                }
                this._propItemData = this._buyPanel.getToUseItemData();
                if (this._propItemData) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetChangePetModel, {"petMainUid": this._mainPetInfo.uid, "petDeputyUid": this._deputyPetInfo.uid, "uid": this._propItemData.uid, "isDeputy": this._radioGroup.selectedData}));
                }
            }
            return;
        }

        /* internal  */updateDeputyByPetInfo(arg1: SPetInfo): void
        {
            this._deputyPetInfo = arg1;
            if (arg1) 
            {
                this._deputyPetItem.dragSource = arg1;
                this._deputyPetModel.updateModelByModelId(PetUtil.getShowModelId(arg1));
                this._deputyPetModel.visible = true;
                this._deputyPetComboBox.selectedIndex = this.getPetInComboIndex(arg1);
                this._deputyPetComboBox.textColor = PetColor.getTalentColor(arg1).intColor;
            }
            else 
            {
                this._deputyPetModel.visible = false;
            }
            this.onRadioChangeHandler(null);
            return;
        }

        /* internal  */onDeputyMoveInPetHandler(arg1: DragEvent): void
        {
            var loc2=null;
            var loc1=arg1.dragItem as PetItem;
            if (loc1) 
            {
                if (loc1 && loc1 is PetItem) 
                {
                    loc2 = loc1.petInfo;
                    this.moveInDeputyPet(loc2);
                }
            }
            return;
        }

        /* internal  */moveInDeputyPet(arg1: SPetInfo): void
        {
            if (PetUtil.isBattled(arg1)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54200));
                return;
            }
            if (this._mainPetInfo && arg1.uid == this._mainPetInfo.uid) 
            {
                this.onMainPetItemClickHandler(null);
            }
            this.updateDeputyByPetInfo(arg1);
            return;
        }

        /* internal  */onMainMoveInPetHandler(arg1: DragEvent): void
        {
            var loc2=null;
            var loc1=arg1.dragItem as PetItem;
            if (loc1) 
            {
                if (loc1 && loc1 is PetItem) 
                {
                    loc2 = loc1.petInfo;
                    this.moveInMainPet(loc2);
                }
            }
            return;
        }

        /* internal  */moveInMainPet(arg1: SPetInfo): void
        {
            if (PetUtil.isBattled(arg1)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54200));
                return;
            }
            if (this._deputyPetInfo && arg1.uid == this._deputyPetInfo.uid) 
            {
                this.onDeputyPetItemClickHandler(null);
            }
            this.updateMainByPetInfo(arg1);
            return;
        }

        /* internal  */onDeputyPetItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._deputyPetItem.dragSource = null;
            this._deputyPetInfo = null;
            this._deputyPetModel.visible = false;
            this._deputyPetComboBox.selectedIndex = -1;
            this._deputyPetComboBox.textColor = GlobalStyle.color1Uint;
            return;
        }

        /* internal  */onMainPetItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._mainPetItem.dragSource = null;
            this._mainPetInfo = null;
            this._mainPetModel.visible = false;
            this._mainPetComboBox.selectedIndex = -1;
            this._mainPetComboBox.textColor = GlobalStyle.color1Uint;
            return;
        }

        /* internal  */onChangeModelSuccessHandler(arg1: DataEvent): void
        {
            this.clear();
            return;
        }

        public onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetUpdate;
            if (loc1.updateType != 4) 
            {
                if (loc1.updateType == 2) 
                {
                    if (this._mainPetInfo && this._mainPetInfo.uid == loc1.petInfo.uid) 
                    {
                        this.onMainPetItemClickHandler(null);
                    }
                    else if (this._deputyPetInfo && this._deputyPetInfo.uid == loc1.petInfo.uid) 
                    {
                        this.onDeputyPetItemClickHandler(null);
                    }
                }
            }
            else if (this._mainPetInfo && this._mainPetInfo.uid == loc1.petInfo.uid) 
            {
                this.updateMainByPetInfo(loc1.petInfo);
            }
            else if (this._deputyPetInfo && this._deputyPetInfo.uid == loc1.petInfo.uid) 
            {
                this.updateDeputyByPetInfo(loc1.petInfo);
            }
            return;
        }

        /* internal  */getPetComboDp(): DataProvider
        {
            var loc3=null;
            var loc1=new DataProvider();
            var loc2=Cache.instance.pet.petInfos;
            var loc4=0;
            while (loc4 < loc2.length) 
            {
                loc3 = loc2[loc4] as SPetInfo;
                loc1.addItem({"label": loc3.name, "value": loc3});
                ++loc4;
            }
            return loc1;
        }

        /* internal  */resetComboBoxData(): void
        {
            this._petDp = this.getPetComboDp();
            this._mainPetComboBox.dataProvider = this._petDp;
            this._deputyPetComboBox.dataProvider = this._petDp;
            return;
        }

        /* internal  */onMainPetComboxChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._mainPetComboBox.selectedItem.value;
            this.moveInMainPet(loc1);
            return;
        }

        /* internal  */onLinkClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            AIManager.onAutoPathAIControl(this._targetInfo);
            return;
        }

        /* internal  */onDeputyPetComboxChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._deputyPetComboBox.selectedItem.value;
            this.moveInDeputyPet(loc1);
            return;
        }

        /* internal  */getPetInComboIndex(arg1: SPetInfo): int
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc1=-1;
            if (arg1) 
            {
                loc2 = this._mainPetComboBox.dataProvider.toArray();
                loc4 = 0;
                while (loc4 < loc2.length) 
                {
                    if ((loc3 = loc2[loc4].value as SPetInfo) && loc3.uid == arg1.uid) 
                    {
                        loc1 = loc4;
                    }
                    ++loc4;
                }
            }
            return loc1;
        }

        public clear(): void
        {
            this.onMainPetItemClickHandler(null);
            this.onDeputyPetItemClickHandler(null);
            this._deputyRadio.selected = true;
            return;
        }

        protected /* override */ init(): void
        {
            this._targetInfo = Cache.instance.scene.getNpcByEffect(ENpcEffect._ENpcEffectPetModelRecover);
            this._changeProp = ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropPetModelChange, 3, -1, true);
            this._petDp = this.getPetComboDp();
            ObjCreate.createFlowerBg(0, 0, 582, 303, this);
            ObjCreate.createBg(0, 305, 582, 95, this);
            ObjCreate.createTextField(Language.getString(54195), 85, 10, 70, 30, this, new GTextFormat(FontUtil.songtiName, 14, 15788607, true));
            ObjCreate.createBg(82, 28, 38, 38, this, ImagesConst.PackItemBg);
            this._mainPetItem = new PetItem();
            this._mainPetItem.setSize(32, 32);
            this._mainPetItem.move(84, 30);
            this._mainPetItem.addEventListener(flash.events.MouseEvent.CLICK, this.onMainPetItemClickHandler);
            this._mainPetItem.addEventListener(DragEvent.Event_Move_In, this.onMainMoveInPetHandler);
            this.addChild(this._mainPetItem);
            this._mainPetComboBox = ObjCreate.createGComboBox(53, 67, 95, 25, this._petDp, this);
            this._mainPetComboBox.selectedIndex = -1;
            this._mainPetComboBox.prompt = Language.getString(54559);
            this._mainPetComboBox.dropdown.setStyle("cellRenderer", PetComboBoxItemCellRender);
            this._mainPetComboBox.addEventListener(flash.events.Event.CHANGE, this.onMainPetComboxChangeHandler);
            this._buyPanel = new BuyCommonPanel();
            this._buyPanel.updateByShopItemInfo(this._changeProp);
            this._buyPanel.x = 162;
            this._buyPanel.y = 15;
            this.addChild(this._buyPanel);
            ObjCreate.createTextField(Language.getString(54196), 470, 10, 70, 30, this, new GTextFormat(FontUtil.songtiName, 14, 15788607, true));
            ObjCreate.createBg(468, 28, 38, 38, this, ImagesConst.PackItemBg);
            this._deputyPetItem = new PetItem();
            this._deputyPetItem.setSize(32, 32);
            this._deputyPetItem.move(470, 30);
            this._deputyPetItem.addEventListener(flash.events.MouseEvent.CLICK, this.onDeputyPetItemClickHandler);
            this._deputyPetItem.addEventListener(DragEvent.Event_Move_In, this.onDeputyMoveInPetHandler);
            this.addChild(this._deputyPetItem);
            this._deputyPetComboBox = ObjCreate.createGComboBox(437, 67, 95, 25, this._petDp, this);
            this._deputyPetComboBox.selectedIndex = -1;
            this._deputyPetComboBox.dropdown.setStyle("cellRenderer", PetComboBoxItemCellRender);
            this._deputyPetComboBox.prompt = Language.getString(54560);
            this._deputyPetComboBox.addEventListener(flash.events.Event.CHANGE, this.onDeputyPetComboxChangeHandler);
            this._changeSprite = new Object /* flash.display.Sprite */();
            this._changeSprite.x = 50;
            this._changeSprite.y = 110;
            this.addChild(this._changeSprite);
            this._changeSprite.addChild(new GImageBitmap("PetChangeModelBg.swf", 0, 0));
            this._changeSprite.addChild(new GImageBitmap("PetCPreLabel.png", -15, -10));
            this._mainPetModel = new PetModelPanel();
            this._mainPetModel.x = 85;
            this._mainPetModel.y = 140;
            this._mainPetModel.visible = false;
            this._changeSprite.addChild(this._mainPetModel);
            ObjCreate.createBg(185, 3, 115, 35, this._changeSprite, ImagesConst.SelectedBg);
            this._changeSprite.addChild(new GImageBitmap("PetChangePreviewLabel.png", 195, 7));
            this._changeSprite.addChild(new GImageBitmap("PetChangeModelBg.swf", 315, 0));
            this._changeSprite.addChild(new GImageBitmap("PetCAfterLabel.png", 305, -10));
            this._deputyPetModel = new PetModelPanel();
            this._deputyPetModel.x = 402;
            this._deputyPetModel.y = 140;
            this._deputyPetModel.visible = false;
            this._changeSprite.addChild(this._deputyPetModel);
            this._radioGroup = new RadioButtonGroup("main");
            this._mainRadio = ObjCreate.createRadioButton(Language.getString(54195), 205, 60, 100, 28, this._changeSprite, "GCheckBox");
            this._mainRadio.value = 0;
            this._deputyRadio = ObjCreate.createRadioButton(Language.getString(54196), 205, 85, 100, 28, this._changeSprite, "GCheckBox");
            this._deputyRadio.selected = true;
            this._deputyRadio.value = 1;
            this._mainRadio.group = this._radioGroup;
            this._deputyRadio.group = this._radioGroup;
            this._radioGroup.addEventListener(flash.events.Event.CHANGE, this.onRadioChangeHandler);
            this._changeBtn = ObjCreate.createGButton(Language.getString(54198), 205, 120, 70, 28, this._changeSprite, "ButtonSpecial");
            this._changeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onChangeBtnClickHandler);
            super.init();
            updateTip(PublicMessageConfig.instance.getPetForgeTextByName("changeModelDesc"), 10, 315, 82);
            this._tipTxt.addEventListener(flash.events.TextEvent.LINK, this.onLinkClickHandler);
            this._flyFoot = new FlyFootBmp();
            this._flyFoot.x = 222;
            this._flyFoot.y = 365;
            this._flyFoot.addEventListener(flash.events.MouseEvent.CLICK, this.onFlyFootClickHandler);
            this.addChild(this._flyFoot);
            return;
        }

        /* internal  */onComboOpenHandler(arg1: Object /* flash.events.Event */): void
        {
            this.resetComboBoxData();
            return;
        }

        /* internal  */onFlyFootClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            AIManager.onAIControl(this._targetInfo, AIType.AI_Convey);
            return;
        }

        public /* override */ updateByPetInfo(arg1: SPetInfo): void
        {
            if (arg1) 
            {
                if (PetUtil.isBattled(arg1)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54200));
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetCallBackAlert));
                    return;
                }
                if (this._mainPetInfo != null) 
                {
                    if (arg1.uid == this._mainPetInfo.uid) 
                    {
                        if (this._deputyPetInfo == null) 
                        {
                            this.updateDeputyByPetInfo(arg1);
                            this.onMainPetItemClickHandler(null);
                        }
                    }
                    else if (this._deputyPetInfo == null) 
                    {
                        this.updateDeputyByPetInfo(arg1);
                    }
                }
                else if (this._deputyPetInfo) 
                {
                    if (arg1.uid == this._deputyPetInfo.uid) 
                    {
                        this.updateMainByPetInfo(arg1);
                        this.onDeputyPetItemClickHandler(null);
                    }
                    else 
                    {
                        this.updateMainByPetInfo(arg1);
                    }
                }
                else 
                {
                    this.updateMainByPetInfo(arg1);
                }
            }
            else 
            {
                this.clear();
            }
            return;
        }

        /* internal */ /* var */_changeSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_mainPetItem: PetItem;

        /* internal */ /* var */_mainPetComboBox: GComboBox;

        /* internal */ /* var */_mainPetModel: PetModelPanel;

        /* internal */ /* var */_deputyPetComboBox: GComboBox;

        /* internal */ /* var */_deputyPetModel: PetModelPanel;

        /* internal */ /* var */_buyPanel: BuyCommonPanel;

        /* internal */ /* var */_radioGroup: RadioButtonGroup;

        /* internal */ /* var */_mainRadio: GRadioButton;

        /* internal */ /* var */_deputyRadio: GRadioButton;

        /* internal */ /* var */_changeBtn: GButton;

        /* internal */ /* var */_flyFoot: FlyFootBmp;

        /* internal */ /* var */_mainPetInfo: SPetInfo;

        /* internal */ /* var */_deputyPetInfo: SPetInfo;

        /* internal */ /* var */_changeProp: ShopItemInfo;

        /* internal */ /* var */_targetInfo: TaskTargetInfo;

        /* internal */ /* var */_petDp: DataProvider;

        /* internal */ /* var */_shopItemInfo: ShopItemInfo;

        /* internal */ /* var */_deputyPetItem: PetItem;

        /* internal */ /* var */_propItemData: ItemData;
    }
